<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>color.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_color_color.js.html">color.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2006 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utilities related to color and color conversion.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.color&#39;);
<a name="line20"></a>
<a name="line21"></a>goog.require(&#39;goog.color.names&#39;);
<a name="line22"></a>goog.require(&#39;goog.math&#39;);
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>/**
<a name="line26"></a> * Parses a color out of a string.
<a name="line27"></a> * @param {string} str Color in some format.
<a name="line28"></a> * @return {Object} Contains two properties: &#39;hex&#39;, which is a string containing
<a name="line29"></a> *     a hex representation of the color, as well as &#39;type&#39;, which is a string
<a name="line30"></a> *     containing the type of color format passed in (&#39;hex&#39;, &#39;rgb&#39;, &#39;named&#39;).
<a name="line31"></a> */
<a name="line32"></a>goog.color.parse = function(str) {
<a name="line33"></a>  var result = {};
<a name="line34"></a>  str = String(str);
<a name="line35"></a>
<a name="line36"></a>  var maybeHex = goog.color.prependPoundIfNecessary_(str);
<a name="line37"></a>  if (goog.color.isValidHexColor_(maybeHex)) {
<a name="line38"></a>    result.hex = goog.color.normalizeHex(maybeHex);
<a name="line39"></a>    result.type = &#39;hex&#39;;
<a name="line40"></a>    return result;
<a name="line41"></a>  } else {
<a name="line42"></a>    var rgb = goog.color.isValidRgbColor_(str);
<a name="line43"></a>    if (rgb.length) {
<a name="line44"></a>      result.hex = goog.color.rgbArrayToHex(rgb);
<a name="line45"></a>      result.type = &#39;rgb&#39;;
<a name="line46"></a>      return result;
<a name="line47"></a>    } else if (goog.color.names) {
<a name="line48"></a>      var hex = goog.color.names[str.toLowerCase()];
<a name="line49"></a>      if (hex) {
<a name="line50"></a>        result.hex = hex;
<a name="line51"></a>        result.type = &#39;named&#39;;
<a name="line52"></a>        return result;
<a name="line53"></a>      }
<a name="line54"></a>    }
<a name="line55"></a>  }
<a name="line56"></a>  throw Error(str + &#39; is not a valid color string&#39;);
<a name="line57"></a>};
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/**
<a name="line61"></a> * Parses red, green, blue components out of a valid rgb color string.
<a name="line62"></a> * @param {string} str RGB representation of a color.
<a name="line63"></a> *    {@see goog.color.isValidRgbColor_}.
<a name="line64"></a> * @return {!Array.&lt;number&gt;} array containing [r, g, b], each an int in
<a name="line65"></a> *    [0, 255].
<a name="line66"></a> */
<a name="line67"></a>goog.color.parseRgb = function(str) {
<a name="line68"></a>  var rgb = goog.color.isValidRgbColor_(str);
<a name="line69"></a>  if (!rgb.length) {
<a name="line70"></a>    throw Error(str + &#39; is not a valid RGB color&#39;);
<a name="line71"></a>  }
<a name="line72"></a>  return rgb;
<a name="line73"></a>};
<a name="line74"></a>
<a name="line75"></a>
<a name="line76"></a>/**
<a name="line77"></a> * Converts a hex representation of a color to RGB.
<a name="line78"></a> * @param {string} hexColor Color to convert.
<a name="line79"></a> * @return {string} string of the form &#39;rgb(R,G,B)&#39; which can be used in
<a name="line80"></a> *    styles.
<a name="line81"></a> */
<a name="line82"></a>goog.color.hexToRgbStyle = function(hexColor) {
<a name="line83"></a>  return goog.color.rgbStyle_(goog.color.hexToRgb(hexColor));
<a name="line84"></a>};
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * Regular expression for extracting the digits in a hex color triplet.
<a name="line89"></a> * @type {RegExp}
<a name="line90"></a> * @private
<a name="line91"></a> */
<a name="line92"></a>goog.color.hexTripletRe_ = /#(.)(.)(.)/;
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>/**
<a name="line96"></a> * Normalize an hex representation of a color
<a name="line97"></a> * @param {string} hexColor an hex color string.
<a name="line98"></a> * @return {string} hex color in the format &#39;#rrggbb&#39; with all lowercase
<a name="line99"></a> *     literals.
<a name="line100"></a> */
<a name="line101"></a>goog.color.normalizeHex = function(hexColor) {
<a name="line102"></a>  if (!goog.color.isValidHexColor_(hexColor)) {
<a name="line103"></a>    throw Error(&quot;&#39;&quot; + hexColor + &quot;&#39; is not a valid hex color&quot;);
<a name="line104"></a>  }
<a name="line105"></a>  if (hexColor.length == 4) { // of the form #RGB
<a name="line106"></a>    hexColor = hexColor.replace(goog.color.hexTripletRe_, &#39;#$1$1$2$2$3$3&#39;);
<a name="line107"></a>  }
<a name="line108"></a>  return hexColor.toLowerCase();
<a name="line109"></a>};
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>/**
<a name="line113"></a> * Converts a hex representation of a color to RGB.
<a name="line114"></a> * @param {string} hexColor Color to convert.
<a name="line115"></a> * @return {!Array} array containing [r, g, b] as ints in [0, 255].
<a name="line116"></a> */
<a name="line117"></a>goog.color.hexToRgb = function(hexColor) {
<a name="line118"></a>  hexColor = goog.color.normalizeHex(hexColor);
<a name="line119"></a>  var r = parseInt(hexColor.substr(1, 2), 16);
<a name="line120"></a>  var g = parseInt(hexColor.substr(3, 2), 16);
<a name="line121"></a>  var b = parseInt(hexColor.substr(5, 2), 16);
<a name="line122"></a>
<a name="line123"></a>  return [r, g, b];
<a name="line124"></a>};
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/**
<a name="line128"></a> * Converts a color from RGB to hex representation.
<a name="line129"></a> * @param {number} r Amount of red, int between 0 and 255.
<a name="line130"></a> * @param {number} g Amount of green, int between 0 and 255.
<a name="line131"></a> * @param {number} b Amount of blue, int between 0 and 255.
<a name="line132"></a> * @return {string} hex representation of the color.
<a name="line133"></a> */
<a name="line134"></a>goog.color.rgbToHex = function(r, g, b) {
<a name="line135"></a>  r = Number(r);
<a name="line136"></a>  g = Number(g);
<a name="line137"></a>  b = Number(b);
<a name="line138"></a>  if (isNaN(r) || r &lt; 0 || r &gt; 255 ||
<a name="line139"></a>      isNaN(g) || g &lt; 0 || g &gt; 255 ||
<a name="line140"></a>      isNaN(b) || b &lt; 0 || b &gt; 255) {
<a name="line141"></a>    throw Error(&#39;&quot;(&#39; + r + &#39;,&#39; + g + &#39;,&#39; + b + &#39;&quot;) is not a valid RGB color&#39;);
<a name="line142"></a>  }
<a name="line143"></a>  var hexR = goog.color.prependZeroIfNecessary_(r.toString(16));
<a name="line144"></a>  var hexG = goog.color.prependZeroIfNecessary_(g.toString(16));
<a name="line145"></a>  var hexB = goog.color.prependZeroIfNecessary_(b.toString(16));
<a name="line146"></a>  return &#39;#&#39; + hexR + hexG + hexB;
<a name="line147"></a>};
<a name="line148"></a>
<a name="line149"></a>
<a name="line150"></a>/**
<a name="line151"></a> * Converts a color from RGB to hex representation.
<a name="line152"></a> * @param {Array.&lt;number&gt;} rgb Array of [r, g, b], with each value in [0, 255].
<a name="line153"></a> * @return {string} hex representation of the color.
<a name="line154"></a> */
<a name="line155"></a>goog.color.rgbArrayToHex = function(rgb) {
<a name="line156"></a>  return goog.color.rgbToHex(rgb[0], rgb[1], rgb[2]);
<a name="line157"></a>};
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * Converts a color from RGB color space to HSL color space.
<a name="line162"></a> * Modified from {@link http://en.wikipedia.org/wiki/HLS_color_space}.
<a name="line163"></a> * @param {number} r Value of red, in [0, 255].
<a name="line164"></a> * @param {number} g Value of green, in [0, 255].
<a name="line165"></a> * @param {number} b Value of blue, in [0, 255].
<a name="line166"></a> * @return {!Array.&lt;number&gt;} [h, s, l] values for the color, with h an int in
<a name="line167"></a> *    [0, 360] and s and l in [0, 1].
<a name="line168"></a> */
<a name="line169"></a>goog.color.rgbToHsl = function(r, g, b) {
<a name="line170"></a>  // First must normalize r, g, b to be between 0 and 1.
<a name="line171"></a>  var normR = r / 255;
<a name="line172"></a>  var normG = g / 255;
<a name="line173"></a>  var normB = b / 255;
<a name="line174"></a>  var max = Math.max(normR, normG, normB);
<a name="line175"></a>  var min = Math.min(normR, normG, normB);
<a name="line176"></a>  var h = 0;
<a name="line177"></a>  var s = 0;
<a name="line178"></a>
<a name="line179"></a>  // Luminosity is the average of the max and min rgb color intensities.
<a name="line180"></a>  var l = 0.5 * (max + min);
<a name="line181"></a>
<a name="line182"></a>  // The hue and saturation are dependent on which color intensity is the max.
<a name="line183"></a>  // If max and min are equal, the color is gray and h and s should be 0.
<a name="line184"></a>  if (max != min) {
<a name="line185"></a>    if (max == normR) {
<a name="line186"></a>      h = 60 * (normG - normB) / (max - min);
<a name="line187"></a>    } else if (max == normG) {
<a name="line188"></a>      h = 60 * (normB - normR) / (max - min) + 120;
<a name="line189"></a>    } else if (max == normB) {
<a name="line190"></a>      h = 60 * (normR - normG) / (max - min) + 240;
<a name="line191"></a>    }
<a name="line192"></a>
<a name="line193"></a>    if (0 &lt; l &amp;&amp; l &lt;= 0.5) {
<a name="line194"></a>      s = (max - min) / (2 * l);
<a name="line195"></a>    } else {
<a name="line196"></a>      s = (max - min) / (2 - 2 * l);
<a name="line197"></a>    }
<a name="line198"></a>  }
<a name="line199"></a>
<a name="line200"></a>  // Make sure the hue falls between 0 and 360.
<a name="line201"></a>  return [Math.round(h + 360) % 360, s, l];
<a name="line202"></a>};
<a name="line203"></a>
<a name="line204"></a>
<a name="line205"></a>/**
<a name="line206"></a> * Converts a color from RGB color space to HSL color space.
<a name="line207"></a> * @param {Array.&lt;number&gt;} rgb [r, g, b] values for the color, each in [0, 255].
<a name="line208"></a> * @return {!Array.&lt;number&gt;} [h, s, l] values for the color, with h in [0, 360]
<a name="line209"></a> *    and s and l in [0, 1].
<a name="line210"></a> */
<a name="line211"></a>goog.color.rgbArrayToHsl = function(rgb) {
<a name="line212"></a>  return goog.color.rgbToHsl(rgb[0], rgb[1], rgb[2]);
<a name="line213"></a>};
<a name="line214"></a>
<a name="line215"></a>/**
<a name="line216"></a> * Helper for hslToRgb.
<a name="line217"></a> * @param {number} v1 Helper variable 1.
<a name="line218"></a> * @param {number} v2 Helper variable 2.
<a name="line219"></a> * @param {number} vH Helper variable 3.
<a name="line220"></a> * @return {number} Appropriate RGB value, given the above.
<a name="line221"></a> * @private
<a name="line222"></a> */
<a name="line223"></a>goog.color.hueToRgb_ = function(v1, v2, vH) {
<a name="line224"></a>  if (vH &lt; 0) {
<a name="line225"></a>    vH += 1;
<a name="line226"></a>  } else if (vH &gt; 1) {
<a name="line227"></a>    vH -= 1;
<a name="line228"></a>  }
<a name="line229"></a>  if ((6 * vH) &lt; 1) {
<a name="line230"></a>    return (v1 + (v2 - v1) * 6 * vH);
<a name="line231"></a>  } else if (2 * vH &lt; 1) {
<a name="line232"></a>    return v2;
<a name="line233"></a>  } else if (3 * vH &lt; 2) {
<a name="line234"></a>    return (v1 + (v2 - v1) * ((2 / 3) - vH) * 6);
<a name="line235"></a>  }
<a name="line236"></a>  return v1;
<a name="line237"></a>};
<a name="line238"></a>
<a name="line239"></a>
<a name="line240"></a>/**
<a name="line241"></a> * Converts a color from HSL color space to RGB color space.
<a name="line242"></a> * Modified from {@link http://www.easyrgb.com/math.html}
<a name="line243"></a> * @param {number} h Hue, in [0, 360].
<a name="line244"></a> * @param {number} s Saturation, in [0, 1].
<a name="line245"></a> * @param {number} l Luminosity, in [0, 1].
<a name="line246"></a> * @return {!Array.&lt;number&gt;} [r, g, b] values for the color, with each an int in
<a name="line247"></a> *    [0, 255].
<a name="line248"></a> */
<a name="line249"></a>goog.color.hslToRgb = function(h, s, l) {
<a name="line250"></a>  var r = 0;
<a name="line251"></a>  var g = 0;
<a name="line252"></a>  var b = 0;
<a name="line253"></a>  var normH = h / 360; // normalize h to fall in [0, 1]
<a name="line254"></a>
<a name="line255"></a>  if (s == 0) {
<a name="line256"></a>    r = g = b = l * 255;
<a name="line257"></a>  } else {
<a name="line258"></a>    var temp1 = 0;
<a name="line259"></a>    var temp2 = 0;
<a name="line260"></a>    if (l &lt; 0.5) {
<a name="line261"></a>      temp2 = l * (1 + s);
<a name="line262"></a>    } else {
<a name="line263"></a>      temp2 = l + s - (s * l);
<a name="line264"></a>    }
<a name="line265"></a>    temp1 = 2 * l - temp2;
<a name="line266"></a>    r = 255 * goog.color.hueToRgb_(temp1, temp2, normH + (1 / 3));
<a name="line267"></a>    g = 255 * goog.color.hueToRgb_(temp1, temp2, normH);
<a name="line268"></a>    b = 255 * goog.color.hueToRgb_(temp1, temp2, normH - (1 / 3));
<a name="line269"></a>  }
<a name="line270"></a>
<a name="line271"></a>  return [Math.round(r), Math.round(g), Math.round(b)];
<a name="line272"></a>};
<a name="line273"></a>
<a name="line274"></a>
<a name="line275"></a>/**
<a name="line276"></a> * Converts a color from HSL color space to RGB color space.
<a name="line277"></a> * @param {Array.&lt;number&gt;} hsl HSL values for the color, h in [0, 360], s and l
<a name="line278"></a> *    in [0, 1].
<a name="line279"></a> * @return {!Array.&lt;number&gt;} [r, g, b] values for the color, with each an int in
<a name="line280"></a> *    [0, 255].
<a name="line281"></a> */
<a name="line282"></a>goog.color.hslArrayToRgb = function(hsl) {
<a name="line283"></a>  return goog.color.hslToRgb(hsl[0], hsl[1], hsl[2]);
<a name="line284"></a>};
<a name="line285"></a>
<a name="line286"></a>
<a name="line287"></a>/**
<a name="line288"></a> * Helper for isValidHexColor_.
<a name="line289"></a> * @type {RegExp}
<a name="line290"></a> * @private
<a name="line291"></a> */
<a name="line292"></a>goog.color.validHexColorRe_ = /^#(?:[0-9a-f]{3}){1,2}$/i;
<a name="line293"></a>
<a name="line294"></a>/**
<a name="line295"></a> * Checks if a string is a valid hex color.  We expect strings of the format
<a name="line296"></a> * #RRGGBB (ex: #1b3d5f) or #RGB (ex: #3CA == #33CCAA).
<a name="line297"></a> * @param {string} str String to check.
<a name="line298"></a> * @return {boolean} Whether the string is a valid hex color.
<a name="line299"></a> * @private
<a name="line300"></a> */
<a name="line301"></a>goog.color.isValidHexColor_ = function(str) {
<a name="line302"></a>  return goog.color.validHexColorRe_.test(str);
<a name="line303"></a>};
<a name="line304"></a>
<a name="line305"></a>/**
<a name="line306"></a> * Helper for isNormalizedHexColor_.
<a name="line307"></a> * @type {RegExp}
<a name="line308"></a> * @private
<a name="line309"></a> */
<a name="line310"></a>goog.color.normalizedHexColorRe_ = /^#[0-9a-f]{6}$/;
<a name="line311"></a>
<a name="line312"></a>/**
<a name="line313"></a> * Checks if a string is a normalized hex color.
<a name="line314"></a> * We expect strings of the format #RRGGBB (ex: #1b3d5f)
<a name="line315"></a> * using only lowercase letters.
<a name="line316"></a> * @param {string} str String to check.
<a name="line317"></a> * @return {boolean} Whether the string is a normalized hex color.
<a name="line318"></a> * @private
<a name="line319"></a> */
<a name="line320"></a>goog.color.isNormalizedHexColor_ = function(str) {
<a name="line321"></a>  return goog.color.normalizedHexColorRe_.test(str);
<a name="line322"></a>};
<a name="line323"></a>
<a name="line324"></a>
<a name="line325"></a>/**
<a name="line326"></a> * Regular expression for matching and capturing RGB style strings. Helper for
<a name="line327"></a> * isValidRgbColor_.
<a name="line328"></a> * @type {RegExp}
<a name="line329"></a> * @private
<a name="line330"></a> */
<a name="line331"></a>goog.color.rgbColorRe_ =
<a name="line332"></a>   /^(?:rgb)?\((0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2})\)$/i;
<a name="line333"></a>
<a name="line334"></a>/**
<a name="line335"></a> * Checks if a string is a valid rgb color.  We expect strings of the format
<a name="line336"></a> * &#39;(r, g, b)&#39;, or &#39;rgb(r, g, b)&#39;, where each color component is an int in
<a name="line337"></a> * [0, 255].
<a name="line338"></a> * @param {string} str String to check.
<a name="line339"></a> * @return {!Array.&lt;number&gt;} the integers [r, g, b] for valid colors or the
<a name="line340"></a> *    empty array for invalid colors.
<a name="line341"></a> * @private
<a name="line342"></a> */
<a name="line343"></a>goog.color.isValidRgbColor_ = function(str) {
<a name="line344"></a>  // Each component is separate (rather than using a repeater) so we can
<a name="line345"></a>  // capture the match. Also, we explicitly set each component to be either 0,
<a name="line346"></a>  // or start with a non-zero, to prevent octal numbers from slipping through.
<a name="line347"></a>  var regExpResultArray = str.match(goog.color.rgbColorRe_);
<a name="line348"></a>  if (regExpResultArray) {
<a name="line349"></a>    var r = Number(regExpResultArray[1]);
<a name="line350"></a>    var g = Number(regExpResultArray[2]);
<a name="line351"></a>    var b = Number(regExpResultArray[3]);
<a name="line352"></a>    if (r &gt;= 0 &amp;&amp; r &lt;= 255 &amp;&amp;
<a name="line353"></a>        g &gt;= 0 &amp;&amp; g &lt;= 255 &amp;&amp;
<a name="line354"></a>        b &gt;= 0 &amp;&amp; b &lt;= 255) {
<a name="line355"></a>      return [r, g, b];
<a name="line356"></a>    }
<a name="line357"></a>  }
<a name="line358"></a>  return [];
<a name="line359"></a>};
<a name="line360"></a>
<a name="line361"></a>
<a name="line362"></a>/**
<a name="line363"></a> * Takes a hex value and prepends a zero if it&#39;s a single digit.
<a name="line364"></a> * @param {string} hex Hex value to prepend if single digit.
<a name="line365"></a> * @return {string} hex value prepended with zero if it was single digit,
<a name="line366"></a> *    otherwise the same value that was passed in.
<a name="line367"></a> * @private
<a name="line368"></a> */
<a name="line369"></a>goog.color.prependZeroIfNecessary_ = function(hex) {
<a name="line370"></a>  return hex.length == 1 ? &#39;0&#39; + hex : hex;
<a name="line371"></a>};
<a name="line372"></a>
<a name="line373"></a>
<a name="line374"></a>/**
<a name="line375"></a> * Takes a string a prepends a &#39;#&#39; sign if one doesn&#39;t exist.
<a name="line376"></a> * @param {string} str String to check.
<a name="line377"></a> * @return {string} The value passed in, prepended with a &#39;#&#39; if it didn&#39;t
<a name="line378"></a> *    already have one.
<a name="line379"></a> * @private
<a name="line380"></a> */
<a name="line381"></a>goog.color.prependPoundIfNecessary_ = function(str) {
<a name="line382"></a>  return str.charAt(0) == &#39;#&#39; ? str : &#39;#&#39; + str;
<a name="line383"></a>};
<a name="line384"></a>
<a name="line385"></a>
<a name="line386"></a>/**
<a name="line387"></a> * Takes an array of [r, g, b] and converts it into a string appropriate for
<a name="line388"></a> * CSS styles.
<a name="line389"></a> * @param {Array.&lt;number&gt;} rgb [r, g, b] with each value in [0, 255].
<a name="line390"></a> * @return {string} string of the form &#39;rgb(r,g,b)&#39;.
<a name="line391"></a> * @private
<a name="line392"></a> */
<a name="line393"></a>goog.color.rgbStyle_ = function(rgb) {
<a name="line394"></a>  return &#39;rgb(&#39; + rgb.join(&#39;,&#39;) + &#39;)&#39;;
<a name="line395"></a>};
<a name="line396"></a>
<a name="line397"></a>
<a name="line398"></a>/**
<a name="line399"></a> * Converts an HSV triplet to an RGB array.  V is brightness because b is
<a name="line400"></a> *   reserved for blue in RGB.
<a name="line401"></a> * @param {number} h Hue value in [0, 1].
<a name="line402"></a> * @param {number} s Saturation value in [0, 1].
<a name="line403"></a> * @param {number} brightness brightness in [0, 255].
<a name="line404"></a> * @return {!Array.&lt;number&gt;} Array of r,g,b values.
<a name="line405"></a> */
<a name="line406"></a>goog.color.hsvToRgb = function(h, s, brightness) {
<a name="line407"></a>  var red = 0;
<a name="line408"></a>  var green = 0;
<a name="line409"></a>  var blue = 0;
<a name="line410"></a>  if (s == 0) {
<a name="line411"></a>    red = brightness;
<a name="line412"></a>    green = brightness;
<a name="line413"></a>    blue = brightness;
<a name="line414"></a>  } else {
<a name="line415"></a>    var sextant = Math.floor(h / 60);
<a name="line416"></a>    var remainder = (h / 60) - sextant;
<a name="line417"></a>    var val1 = brightness * (1 - s);
<a name="line418"></a>    var val2 = brightness * (1 - (s * remainder));
<a name="line419"></a>    var val3 = brightness * (1 - (s * (1 - remainder)));
<a name="line420"></a>    switch (sextant) {
<a name="line421"></a>      case 1:
<a name="line422"></a>        red = val2;
<a name="line423"></a>        green = brightness;
<a name="line424"></a>        blue = val1;
<a name="line425"></a>        break;
<a name="line426"></a>      case 2:
<a name="line427"></a>        red = val1;
<a name="line428"></a>        green = brightness;
<a name="line429"></a>        blue = val3;
<a name="line430"></a>        break;
<a name="line431"></a>      case 3:
<a name="line432"></a>        red = val1;
<a name="line433"></a>        green = val2;
<a name="line434"></a>        blue = brightness;
<a name="line435"></a>        break;
<a name="line436"></a>      case 4:
<a name="line437"></a>        red = val3;
<a name="line438"></a>        green = val1;
<a name="line439"></a>        blue = brightness;
<a name="line440"></a>        break;
<a name="line441"></a>      case 5:
<a name="line442"></a>        red = brightness;
<a name="line443"></a>        green = val1;
<a name="line444"></a>        blue = val2;
<a name="line445"></a>        break;
<a name="line446"></a>      case 6:
<a name="line447"></a>      case 0:
<a name="line448"></a>        red = brightness;
<a name="line449"></a>        green = val3;
<a name="line450"></a>        blue = val1;
<a name="line451"></a>        break;
<a name="line452"></a>    }
<a name="line453"></a>  }
<a name="line454"></a>
<a name="line455"></a>  return [Math.floor(red), Math.floor(green), Math.floor(blue)];
<a name="line456"></a>};
<a name="line457"></a>
<a name="line458"></a>/**
<a name="line459"></a> * Converts from RGB values to an array of HSV values.
<a name="line460"></a> * @param {number} red Red value.
<a name="line461"></a> * @param {number} green Green value.
<a name="line462"></a> * @param {number} blue Blue value.
<a name="line463"></a> * @return {!Array.&lt;number&gt;} array of HSV values.
<a name="line464"></a> */
<a name="line465"></a>goog.color.rgbToHsv = function(red, green, blue) {
<a name="line466"></a>
<a name="line467"></a>  var max = Math.max(Math.max(red, green), blue);
<a name="line468"></a>  var min = Math.min(Math.min(red, green), blue);
<a name="line469"></a>  var hue;
<a name="line470"></a>  var saturation;
<a name="line471"></a>  var value = max;
<a name="line472"></a>  if (min == max) {
<a name="line473"></a>    hue = 0;
<a name="line474"></a>    saturation = 0;
<a name="line475"></a>  } else {
<a name="line476"></a>    var delta = (max - min);
<a name="line477"></a>    saturation = delta / max;
<a name="line478"></a>
<a name="line479"></a>    if (red == max) {
<a name="line480"></a>      hue = (green - blue) / delta;
<a name="line481"></a>    } else if (green == max) {
<a name="line482"></a>      hue = 2 + ((blue - red) / delta);
<a name="line483"></a>    } else {
<a name="line484"></a>      hue = 4 + ((red - green) / delta);
<a name="line485"></a>    }
<a name="line486"></a>    hue *= 60;
<a name="line487"></a>    if (hue &lt; 0) {
<a name="line488"></a>      hue += 360;
<a name="line489"></a>    }
<a name="line490"></a>    if (hue &gt; 360) {
<a name="line491"></a>      hue -= 360;
<a name="line492"></a>    }
<a name="line493"></a>  }
<a name="line494"></a>
<a name="line495"></a>  return [hue, saturation, value];
<a name="line496"></a>};
<a name="line497"></a>
<a name="line498"></a>/**
<a name="line499"></a> * Converts from r,g,b values to an array of HSV values
<a name="line500"></a> * @param {Array.&lt;number&gt;} rgb RGB array.
<a name="line501"></a> * @return {!Array.&lt;number&gt;} array of HSV values.
<a name="line502"></a> */
<a name="line503"></a>goog.color.rgbArrayToHsv = function(rgb) {
<a name="line504"></a>  return goog.color.rgbToHsv(rgb[0], rgb[1], rgb[2]);
<a name="line505"></a>};
<a name="line506"></a>
<a name="line507"></a>/**
<a name="line508"></a> * Converts an HSV triplet to an RGB array
<a name="line509"></a> * @param {Array.&lt;number&gt;} hsv Array of HSV values.
<a name="line510"></a> * @return {!Array.&lt;number&gt;} Array of r,g,b values.
<a name="line511"></a> */
<a name="line512"></a>goog.color.hsvArrayToRgb = function(hsv) {
<a name="line513"></a>  return goog.color.hsvToRgb(hsv[0], hsv[1], hsv[2]);
<a name="line514"></a>};
<a name="line515"></a>
<a name="line516"></a>/**
<a name="line517"></a> * Converts a hex representation of a color to HSL.
<a name="line518"></a> * @param {string} hex Color to convert.
<a name="line519"></a> * @return {!Array.&lt;number&gt;} [h, s, l] values for the color, with h an int in
<a name="line520"></a> *    [0, 360] and s and l in [0, 1].
<a name="line521"></a> */
<a name="line522"></a>goog.color.hexToHsl = function(hex) {
<a name="line523"></a>  var rgb = goog.color.hexToRgb(hex);
<a name="line524"></a>  return goog.color.rgbToHsl(rgb[0], rgb[1], rgb[2]);
<a name="line525"></a>};
<a name="line526"></a>
<a name="line527"></a>/**
<a name="line528"></a> * Converts from h,s,l values to a hex string
<a name="line529"></a> * @param {number} h Hue, in [0, 360].
<a name="line530"></a> * @param {number} s Saturation, in [0, 1].
<a name="line531"></a> * @param {number} l Luminosity, in [0, 1].
<a name="line532"></a> * @return {string} hex representation of the color.
<a name="line533"></a> */
<a name="line534"></a>goog.color.hslToHex = function(h, s, l) {
<a name="line535"></a>  return goog.color.rgbArrayToHex(goog.color.hslToRgb(h, s, l));
<a name="line536"></a>};
<a name="line537"></a>
<a name="line538"></a>/**
<a name="line539"></a> * Converts from an hsl array to a hex string
<a name="line540"></a> * @param {Array.&lt;number&gt;} hsl Array of [h, s, l], in
<a name="line541"></a> *     [[0, 360], [0, 1], [0, 1]].
<a name="line542"></a> * @return {string} hex representation of the color.
<a name="line543"></a> */
<a name="line544"></a>goog.color.hslArrayToHex = function(hsl) {
<a name="line545"></a>  return goog.color.rgbArrayToHex(goog.color.hslToRgb(hsl[0], hsl[1], hsl[2]));
<a name="line546"></a>};
<a name="line547"></a>
<a name="line548"></a>/**
<a name="line549"></a> * Converts a hex representation of a color to HSV
<a name="line550"></a> * @param {string} hex Color to convert.
<a name="line551"></a> * @return {!Array.&lt;number&gt;} [h, s, v] in [[0, 1], [0, 1], [0, 255]].
<a name="line552"></a> */
<a name="line553"></a>goog.color.hexToHsv = function(hex) {
<a name="line554"></a>  return goog.color.rgbArrayToHsv(goog.color.hexToRgb(hex));
<a name="line555"></a>};
<a name="line556"></a>
<a name="line557"></a>/**
<a name="line558"></a> * Converts from h,s,v values to a hex string
<a name="line559"></a> * @param {number} h Hue, in [0, 1].
<a name="line560"></a> * @param {number} s Saturation, in [0, 1].
<a name="line561"></a> * @param {number} v Value, in [0, 255].
<a name="line562"></a> * @return {string} hex representation of the color.
<a name="line563"></a> */
<a name="line564"></a>goog.color.hsvToHex = function(h, s, v) {
<a name="line565"></a>  return goog.color.rgbArrayToHex(goog.color.hsvToRgb(h, s, v));
<a name="line566"></a>};
<a name="line567"></a>
<a name="line568"></a>/**
<a name="line569"></a> * Converts from an HSV array to a hex string
<a name="line570"></a> * @param {Array} hsv Array of [h, s, v] in [[0, 1], [0, 1], [0, 255]].
<a name="line571"></a> * @return {string} hex representation of the color.
<a name="line572"></a> */
<a name="line573"></a>goog.color.hsvArrayToHex = function(hsv) {
<a name="line574"></a>  return goog.color.hsvToHex(hsv[0], hsv[1], hsv[2]);
<a name="line575"></a>};
<a name="line576"></a>
<a name="line577"></a>
<a name="line578"></a>/**
<a name="line579"></a> * Calculates the Euclidean distance between two color vectors on an HSL sphere.
<a name="line580"></a> * A demo of the sphere can be found at:
<a name="line581"></a> * http://en.wikipedia.org/wiki/HSL_color_space
<a name="line582"></a> * In short, a vector for color (H, S, L) in this system can be expressed as
<a name="line583"></a> * (S*L&#39;*cos(2*PI*H), S*L&#39;*sin(2*PI*H), L), where L&#39; = abs(L - 0.5), and we
<a name="line584"></a> * simply calculate the 1-2 distance using these coordinates
<a name="line585"></a> * @param {Array.&lt;number&gt;} hsl1 First color represented by a 3-element array
<a name="line586"></a> *     with hue in range [0, 360], and saturation and luminance values in the
<a name="line587"></a> *     range[0, 1].
<a name="line588"></a> * @param {Array.&lt;number&gt;} hsl2 Second color represented by a 3-element array
<a name="line589"></a> *     with hue in range [0, 360], and saturation and luminance values in the
<a name="line590"></a> *     range[0, 1].
<a name="line591"></a> * @return {number} Distance between the two colors, in the range [0, 1].
<a name="line592"></a> */
<a name="line593"></a>goog.color.hslDistance = function(hsl1, hsl2) {
<a name="line594"></a>  var sl1, sl2;
<a name="line595"></a>  if (hsl1[2] &lt;= 0.5) {
<a name="line596"></a>    sl1 = hsl1[1] * hsl1[2];
<a name="line597"></a>  } else {
<a name="line598"></a>    sl1 = hsl1[1] * (1.0 - hsl1[2]);
<a name="line599"></a>  }
<a name="line600"></a>
<a name="line601"></a>  if (hsl2[2] &lt;= 0.5) {
<a name="line602"></a>    sl2 = hsl2[1] * hsl2[2];
<a name="line603"></a>  } else {
<a name="line604"></a>    sl2 = hsl2[1] * (1.0 - hsl2[2]);
<a name="line605"></a>  }
<a name="line606"></a>
<a name="line607"></a>  var h1 = hsl1[0] / 360.0;
<a name="line608"></a>  var h2 = hsl2[0] / 360.0;
<a name="line609"></a>  var dh = (h1 - h2) * 2.0 * Math.PI;
<a name="line610"></a>  return (hsl1[2] - hsl2[2]) * (hsl1[2] - hsl2[2]) +
<a name="line611"></a>      sl1 * sl1 + sl2 * sl2 - 2 * sl1 * sl2 * Math.cos(dh);
<a name="line612"></a>};
<a name="line613"></a>
<a name="line614"></a>
<a name="line615"></a>/**
<a name="line616"></a> * Blend two colors together, using the specified factor to indicate the weight
<a name="line617"></a> * given to the first color
<a name="line618"></a> * @param {Array.&lt;number&gt;} rgb1 First color represented by a 3-element array
<a name="line619"></a> *     with red, green, and blue values respectively, in the range [0, 255].
<a name="line620"></a> * @param {Array.&lt;number&gt;} rgb2 Second color represented by a 3-element array
<a name="line621"></a> *     with red, green, and blue values respectively, in the range [0, 255].
<a name="line622"></a> * @param {number} factor The weight to be given to rgb1 over rgb2. Values
<a name="line623"></a> *     should be in the range [0, 1]. If less than 0, factor will be set to 0.
<a name="line624"></a> *     If greater than 1, factor will be set to 1.
<a name="line625"></a> * @return {!Array.&lt;number&gt;} Combined color represented by a 3-element
<a name="line626"></a> *     array with red, green, and blue values respectively, in the
<a name="line627"></a> *     range [0, 255].
<a name="line628"></a> */
<a name="line629"></a>goog.color.blend = function(rgb1, rgb2, factor) {
<a name="line630"></a>  factor = goog.math.clamp(factor, 0, 1);
<a name="line631"></a>
<a name="line632"></a>  return [
<a name="line633"></a>    Math.round(factor * rgb1[0] + (1.0 - factor) * rgb2[0]),
<a name="line634"></a>    Math.round(factor * rgb1[1] + (1.0 - factor) * rgb2[1]),
<a name="line635"></a>    Math.round(factor * rgb1[2] + (1.0 - factor) * rgb2[2])
<a name="line636"></a>  ];
<a name="line637"></a>};
<a name="line638"></a>
<a name="line639"></a>
<a name="line640"></a>/**
<a name="line641"></a> * Adds black to the specified color, darkening it
<a name="line642"></a> * @param {Array.&lt;number&gt;} rgb Color represented by a 3-element array with
<a name="line643"></a> *     red, green, and blue values respectively, in the range [0, 255].
<a name="line644"></a> * @param {number} factor Number in the range [0, 1]. 0 will do nothing, while
<a name="line645"></a> *     1 will return black. If less than 0, factor will be set to 0. If greater
<a name="line646"></a> *     than 1, factor will be set to 1.
<a name="line647"></a> * @return {!Array.&lt;number&gt;} Combined color represented by a 3-element array
<a name="line648"></a> *     with red, green, and blue values respectively, in the range [0, 255].
<a name="line649"></a> */
<a name="line650"></a>goog.color.darken = function(rgb, factor) {
<a name="line651"></a>  var black = [0, 0, 0];
<a name="line652"></a>  return goog.color.blend(black, rgb, factor);
<a name="line653"></a>};
<a name="line654"></a>
<a name="line655"></a>
<a name="line656"></a>/**
<a name="line657"></a> * Adds white to the specified color, lightening it
<a name="line658"></a> * @param {Array.&lt;number&gt;} rgb Color represented by a 3-element array with
<a name="line659"></a> *     red, green, and blue values respectively, in the range [0, 255].
<a name="line660"></a> * @param {number} factor Number in the range [0, 1].  0 will do nothing, while
<a name="line661"></a> *     1 will return white. If less than 0, factor will be set to 0. If greater
<a name="line662"></a> *     than 1, factor will be set to 1.
<a name="line663"></a> * @return {!Array.&lt;number&gt;} Combined color represented by a 3-element array
<a name="line664"></a> *     with red, green, and blue values respectively, in the range [0, 255].
<a name="line665"></a> */
<a name="line666"></a>goog.color.lighten = function(rgb, factor) {
<a name="line667"></a>  var white = [255, 255, 255];
<a name="line668"></a>  return goog.color.blend(white, rgb, factor);
<a name="line669"></a>};
<a name="line670"></a>
<a name="line671"></a>
<a name="line672"></a>/**
<a name="line673"></a> * Find the &quot;best&quot; (highest-contrast) of the suggested colors for the prime
<a name="line674"></a> * color. Uses W3C formula for judging readability and visual accessibility:
<a name="line675"></a> * http://www.w3.org/TR/AERT#color-contrast
<a name="line676"></a> * @param {Array.&lt;number&gt;} prime Color represented by a 3-element array with
<a name="line677"></a> *     red, green, and blue values respectively, in the range [0, 255].
<a name="line678"></a> * @param {Array.&lt;Array&gt;} suggestions Array of colors, each a 3-element array
<a name="line679"></a> *     with red, green, and blue values respectively, in the range [0, 255].
<a name="line680"></a> * @return {!Array.&lt;number&gt;} Highest-contrast color represented by a 3-element
<a name="line681"></a> *     array with red, green and blue values respectively, in the range
<a name="line682"></a> *     [0, 255].
<a name="line683"></a> */
<a name="line684"></a>goog.color.highContrast = function(prime, suggestions) {
<a name="line685"></a>  var suggestionsWithDiff = [];
<a name="line686"></a>  for (var i = 0; i &lt; suggestions.length; i++) {
<a name="line687"></a>    suggestionsWithDiff.push({
<a name="line688"></a>      color: suggestions[i],
<a name="line689"></a>      diff: goog.color.yiqBrightnessDiff_(suggestions[i], prime) +
<a name="line690"></a>          goog.color.colorDiff_(suggestions[i], prime)
<a name="line691"></a>    });
<a name="line692"></a>  }
<a name="line693"></a>  suggestionsWithDiff.sort(function(a, b) {
<a name="line694"></a>    return b.diff - a.diff;
<a name="line695"></a>  });
<a name="line696"></a>  return suggestionsWithDiff[0].color;
<a name="line697"></a>};
<a name="line698"></a>
<a name="line699"></a>
<a name="line700"></a>/**
<a name="line701"></a> * Calculate brightness of a color according to YIQ formula (brightness is Y).
<a name="line702"></a> * More info on YIQ here: http://en.wikipedia.org/wiki/YIQ. Helper method for
<a name="line703"></a> * goog.color.highContrast()
<a name="line704"></a> * @param {Array.&lt;number&gt;} rgb Color represented by a 3-element array with
<a name="line705"></a> *     red, green, and blue values respectively, in the range [0, 255].
<a name="line706"></a> * @return {number} brightness (Y).
<a name="line707"></a> * @private
<a name="line708"></a> */
<a name="line709"></a>goog.color.yiqBrightness_ = function(rgb) {
<a name="line710"></a>  return Math.round((rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000);
<a name="line711"></a>};
<a name="line712"></a>
<a name="line713"></a>
<a name="line714"></a>/**
<a name="line715"></a> * Calculate difference in brightness of two colors. Helper method for
<a name="line716"></a> * goog.color.highContrast()
<a name="line717"></a> * @param {Array.&lt;number&gt;} rgb1 Color represented by a 3-element array with
<a name="line718"></a> *     red, green, and blue values respectively, in the range [0, 255].
<a name="line719"></a> * @param {Array.&lt;number&gt;} rgb2 Color represented by a 3-element array with
<a name="line720"></a> *     red, green, and blue values respectively, in the range [0, 255].
<a name="line721"></a> * @return {number} Brightness difference.
<a name="line722"></a> * @private
<a name="line723"></a> */
<a name="line724"></a>goog.color.yiqBrightnessDiff_ = function(rgb1, rgb2) {
<a name="line725"></a>  return Math.abs(goog.color.yiqBrightness_(rgb1) -
<a name="line726"></a>                  goog.color.yiqBrightness_(rgb2));
<a name="line727"></a>};
<a name="line728"></a>
<a name="line729"></a>
<a name="line730"></a>/**
<a name="line731"></a> * Calculate color difference between two colors. Helper method for
<a name="line732"></a> * goog.color.highContrast()
<a name="line733"></a> * @param {Array.&lt;number&gt;} rgb1 Color represented by a 3-element array with
<a name="line734"></a> *     red, green, and blue values respectively, in the range [0, 255].
<a name="line735"></a> * @param {Array.&lt;number&gt;} rgb2 Color represented by a 3-element array with
<a name="line736"></a> *     red, green, and blue values respectively, in the range [0, 255].
<a name="line737"></a> * @return {number} Color difference.
<a name="line738"></a> * @private
<a name="line739"></a> */
<a name="line740"></a>goog.color.colorDiff_ = function(rgb1, rgb2) {
<a name="line741"></a>  return Math.abs(rgb1[0] - rgb2[0]) + Math.abs(rgb1[1] - rgb2[1]) +
<a name="line742"></a>      Math.abs(rgb1[2] - rgb2[2]);
<a name="line743"></a>};
</pre>


</body>
</html>
